/*     */ package blockbuster.math.molang;
/*     */ 
/*     */ import blockbuster.math.Constant;
/*     */ import blockbuster.math.IValue;
/*     */ import blockbuster.math.MathBuilder;
/*     */ import blockbuster.math.Variable;
/*     */ import blockbuster.math.molang.expressions.MolangAssignment;
/*     */ import blockbuster.math.molang.expressions.MolangExpression;
/*     */ import blockbuster.math.molang.expressions.MolangMultiStatement;
/*     */ import blockbuster.math.molang.expressions.MolangValue;
/*     */ import blockbuster.math.molang.functions.AcosDegrees;
/*     */ import blockbuster.math.molang.functions.AsinDegrees;
/*     */ import blockbuster.math.molang.functions.Atan2Degrees;
/*     */ import blockbuster.math.molang.functions.AtanDegrees;
/*     */ import blockbuster.math.molang.functions.CosDegrees;
/*     */ import blockbuster.math.molang.functions.SinDegrees;
/*     */ import com.google.gson.JsonElement;
/*     */ import com.google.gson.JsonPrimitive;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MolangParser
/*     */   extends MathBuilder
/*     */ {
/*  32 */   public static final MolangExpression ZERO = (MolangExpression)new MolangValue(null, (IValue)new Constant(0.0D));
/*  33 */   public static final MolangExpression ONE = (MolangExpression)new MolangValue(null, (IValue)new Constant(1.0D));
/*     */ 
/*     */   
/*     */   public static final String RETURN = "return ";
/*     */   
/*     */   private MolangMultiStatement currentStatement;
/*     */   
/*     */   private boolean registerAsGlobals;
/*     */ 
/*     */   
/*     */   public MolangParser() {
/*  44 */     this.functions.put("cos", CosDegrees.class);
/*  45 */     this.functions.put("sin", SinDegrees.class);
/*  46 */     this.functions.put("acos", AcosDegrees.class);
/*  47 */     this.functions.put("asin", AsinDegrees.class);
/*  48 */     this.functions.put("atan", AtanDegrees.class);
/*  49 */     this.functions.put("atan2", Atan2Degrees.class);
/*     */ 
/*     */     
/*  52 */     remap("abs", "math.abs");
/*  53 */     remap("ceil", "math.ceil");
/*  54 */     remap("clamp", "math.clamp");
/*  55 */     remap("cos", "math.cos");
/*  56 */     remap("exp", "math.exp");
/*  57 */     remap("floor", "math.floor");
/*  58 */     remap("lerp", "math.lerp");
/*  59 */     remap("lerprotate", "math.lerprotate");
/*  60 */     remap("ln", "math.ln");
/*  61 */     remap("max", "math.max");
/*  62 */     remap("min", "math.min");
/*  63 */     remap("mod", "math.mod");
/*  64 */     remap("pow", "math.pow");
/*  65 */     remap("random", "math.random");
/*  66 */     remap("round", "math.round");
/*  67 */     remap("sin", "math.sin");
/*  68 */     remap("sqrt", "math.sqrt");
/*  69 */     remap("trunc", "math.trunc");
/*     */ 
/*     */     
/*  72 */     remap("acos", "math.acos");
/*  73 */     remap("asin", "math.asin");
/*  74 */     remap("atan", "math.atan");
/*  75 */     remap("atan2", "math.atan2");
/*  76 */     remap("randomi", "math.random_integer");
/*  77 */     remap("roll", "math.die_roll");
/*  78 */     remap("rolli", "math.die_roll_integer");
/*  79 */     remap("hermite", "math.hermite_blend");
/*     */ 
/*     */     
/*  82 */     remapVar("PI", "math.pi");
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void remap(String old, String newName) {
/*  90 */     this.functions.put(newName, this.functions.remove(old));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void remapVar(String old, String newName) {
/*  98 */     this.variables.put(newName, this.variables.remove(old));
/*     */   }
/*     */ 
/*     */   
/*     */   public void setValue(String name, double value) {
/* 103 */     Variable variable = getVariable(name);
/*     */     
/* 105 */     if (variable != null)
/*     */     {
/* 107 */       variable.set(value);
/*     */     }
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   protected Variable getVariable(String name) {
/* 117 */     Variable variable = (this.currentStatement == null) ? null : (Variable)this.currentStatement.locals.get(name);
/*     */     
/* 119 */     if (variable == null)
/*     */     {
/* 121 */       variable = super.getVariable(name);
/*     */     }
/*     */     
/* 124 */     if (variable == null) {
/*     */       
/* 126 */       variable = new Variable(name, 0.0D);
/*     */       
/* 128 */       register(variable);
/*     */     } 
/*     */     
/* 131 */     return variable;
/*     */   }
/*     */ 
/*     */   
/*     */   public MolangExpression parseJson(JsonElement element) throws MolangException {
/* 136 */     if (element.isJsonPrimitive()) {
/*     */       
/* 138 */       JsonPrimitive primitive = element.getAsJsonPrimitive();
/*     */       
/* 140 */       if (primitive.isString()) {
/*     */         
/*     */         try {
/*     */           
/* 144 */           return (MolangExpression)new MolangValue(this, (IValue)new Constant(Double.parseDouble(primitive.getAsString())));
/*     */         }
/* 146 */         catch (Exception exception) {
/*     */ 
/*     */           
/* 149 */           return parseExpression(primitive.getAsString());
/*     */         } 
/*     */       }
/*     */       
/* 153 */       return (MolangExpression)new MolangValue(this, (IValue)new Constant(primitive.getAsDouble()));
/*     */     } 
/*     */ 
/*     */     
/* 157 */     return ZERO;
/*     */   }
/*     */ 
/*     */   
/*     */   public MolangExpression parseGlobalJson(JsonElement element) throws MolangException {
/* 162 */     this.registerAsGlobals = true;
/*     */     
/* 164 */     MolangExpression expression = parseJson(element);
/*     */     
/* 166 */     this.registerAsGlobals = false;
/*     */     
/* 168 */     return expression;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public MolangExpression parseExpression(String expression) throws MolangException {
/* 176 */     List<String> lines = new ArrayList<>();
/*     */     
/* 178 */     for (String split : expression.toLowerCase().trim().split(";")) {
/*     */       
/* 180 */       if (!split.trim().isEmpty())
/*     */       {
/* 182 */         lines.add(split);
/*     */       }
/*     */     } 
/*     */     
/* 186 */     if (lines.size() == 0)
/*     */     {
/* 188 */       throw new MolangException("Molang expression cannot be blank!");
/*     */     }
/*     */     
/* 191 */     MolangMultiStatement result = new MolangMultiStatement(this);
/*     */     
/* 193 */     this.currentStatement = result;
/*     */ 
/*     */     
/*     */     try {
/* 197 */       for (String line : lines)
/*     */       {
/* 199 */         result.expressions.add(parseOneLine(line));
/*     */       }
/*     */     }
/* 202 */     catch (Exception e) {
/*     */       
/* 204 */       this.currentStatement = null;
/*     */       
/* 206 */       throw e;
/*     */     } 
/*     */     
/* 209 */     this.currentStatement = null;
/*     */     
/* 211 */     return (MolangExpression)result;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   protected MolangExpression parseOneLine(String expression) throws MolangException {
/* 219 */     expression = expression.trim();
/*     */     
/* 221 */     if (expression.startsWith("return ")) {
/*     */       
/*     */       try {
/*     */         
/* 225 */         return (new MolangValue(this, parse(expression.substring("return ".length())))).addReturn();
/*     */       }
/* 227 */       catch (Exception e) {
/*     */         
/* 229 */         throw new MolangException("Couldn't parse return '" + expression + "' expression!");
/*     */       } 
/*     */     }
/*     */ 
/*     */     
/*     */     try {
/* 235 */       List<Object> symbols = breakdownChars(breakdown(expression));
/*     */ 
/*     */       
/* 238 */       if (symbols.size() >= 3 && symbols.get(0) instanceof String && isVariable(symbols.get(0)) && symbols.get(1).equals("=")) {
/*     */         
/* 240 */         String name = (String)symbols.get(0);
/* 241 */         symbols = symbols.subList(2, symbols.size());
/*     */         
/* 243 */         Variable variable = null;
/*     */         
/* 245 */         if (!this.registerAsGlobals && !this.variables.containsKey(name) && !this.currentStatement.locals.containsKey(name)) {
/*     */           
/* 247 */           variable = new Variable(name, 0.0D);
/* 248 */           this.currentStatement.locals.put(name, variable);
/*     */         }
/*     */         else {
/*     */           
/* 252 */           variable = getVariable(name);
/*     */         } 
/*     */         
/* 255 */         return (MolangExpression)new MolangAssignment(this, variable, parseSymbolsMolang(symbols));
/*     */       } 
/*     */       
/* 258 */       return (MolangExpression)new MolangValue(this, parseSymbolsMolang(symbols));
/*     */     }
/* 260 */     catch (Exception e) {
/*     */       
/* 262 */       throw new MolangException("Couldn't parse '" + expression + "' expression!");
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private IValue parseSymbolsMolang(List<Object> symbols) throws MolangException {
/*     */     try {
/* 273 */       return parseSymbols(symbols);
/*     */     }
/* 275 */     catch (Exception e) {
/*     */       
/* 277 */       e.printStackTrace();
/*     */       
/* 279 */       throw new MolangException("Couldn't parse an expression!");
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   protected boolean isOperator(String s) {
/* 290 */     return (super.isOperator(s) || s.equals("="));
/*     */   }
/*     */ }


/* Location:              D:\fanbian\[D][龙之核心]DragonCore-2.6.0.3.jar!\blockbuster\math\molang\MolangParser.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */